package engine.logic
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Elastic;
	import com.greensock.easing.Linear;
	
	import engine.GameData;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.flag.GameDefine;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.newUi.BottomIndexLogic;
	import engine.newUi.BottomIndexUI;
	import engine.newUi.FunctionGuideUI;
	import engine.newUi.RightTopIndexLogic;
	import engine.newUi.WhisperLogic;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.utils.setTimeout;
	
	import newx.com.ResManager;
	import newx.com.config.StaticIni;

	/**
	 * 功能图标引导
	 * @author gufan
	 */	
	public class FunctionGuideLogic
	{
		private static var self:FunctionGuideLogic;
		private var bind:BindManage;
		private var model:ModelLocator;
		
		/**配置文件*/		
		private var guideObj:Object;
		/**第一次登陆*/
		private var first:Boolean	=	true;
		/**需引导等级*/
		private var guideLevelArr:Array = [];
		/**需特殊引导等级与上面的不可有∩*/
		private var specialLevelArr:Array = [];
		/**是否需引导*/
		private var needGuide:Boolean	=	false;
		
		private var guideArrow:MovieClip;
		
		private var lastLevel:int		=	-1;
		
		private var saveGuideArr:Array	=	[];
		
		public var NowLevel:int	=	-1;
		
		private var isUpEffect:Boolean	=	true;
		
		private var guideShow:MovieClip;
		
		public function FunctionGuideLogic()
		{
			init();
		}
		
		public static function getInstance():FunctionGuideLogic
		{
			if(self == null)
			{
				self = new FunctionGuideLogic();
			}
			return self;
		}
		
		private function init():void
		{
			bind 		=	BindManage.getInstance();
			model 		=	ModelLocator.getInstance();
			bind.BindProp(model.scene.Role,"Level",this,"levelChanged");
			hideBtns();
			var guideLevelStr:String = StaticIni.getIniVar("GuideInfo","levelSort");
			guideLevelArr	=	guideLevelStr.split(",");
			
			var specialLevelStr:String = StaticIni.getIniVar("GuideInfo","specialSort");
			specialLevelArr	=	specialLevelStr.split(",");
			
			guideArrow	=	ResManager.newResClass("custSkin_190") as MovieClip;
			BottomIndexUI.getInstance().bottomBg.addChild(guideArrow);
			guideArrow.visible =false;
			
			model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_YLZG_SWITCHSCENE),onSwitchScene);
			
			guideShow	=	ResManager.newResClass("custSkin_252") as MovieClip;
			BottomIndexUI.getInstance().bottomBg.addChild(guideShow);
			guideShow.stop();
			guideShow.visible	=	false;
		}
		
		private function onSwitchScene(e:CustomMsgEvent):void
		{
			guideArrow.visible = false;
			if(saveGuideArr.length != 0 && GameData.SceneType	==	GameDefine.SceneType_Normal)
			{
				var levelObj:Object	=	null
				while(levelObj == null && saveGuideArr.length != 0)
				{
					NowLevel		=	saveGuideArr.shift();
					levelObj		=	StaticIni.getIniObj("GuideLevel_" + NowLevel);
				}
				setTimeout(function():void
				{
					isUpEffect		=	false;
					levelChanged	=	NowLevel;
				},1000);
			}
		}
		private function hideBtns():void
		{
			var totalBtnSt:String 	=	StaticIni.getIniVar("GuideInfo","btnSort");
			var btnArr:Array		=	totalBtnSt.split(",");
			var btnLen:int			=	btnArr.length;
			for(var i:int=0;i<btnLen;i++)
			{
				var btn:SimpleButton	=	BottomIndexUI.getInstance().bottomBg.getChildByName(btnArr[i]) as SimpleButton;
				btn.visible				=	false;
			}
		}
		
		public function set levelChanged(nowLevel:int):void
		{
			if(nowLevel<1) return;
			if(!first && nowLevel == lastLevel) return;
			if(!first && GameData.SceneType	!=	GameDefine.SceneType_Normal)
			{
				if(saveGuideArr.indexOf(nowLevel) == -1)
				{
					saveGuideArr.push(nowLevel);
				}
				return
			}
			/*if(!first && nowLevel>lastLevel&&isUpEffect)
			{
				model.dispatchEvent(new RoleEffectEvent(RoleEffectEvent.LEVEL_UP));
				model.dispatchEvent(new SoundEvent(SoundEvent.LEVEL_UP));
			}*/
			if(!guideArrow) return;
			guideArrow.visible = false;
			
			guideObj	=	null;
			var levelIndex:int = guideLevelArr.indexOf(""+nowLevel);
			
			while(guideObj == null)
			{
				guideObj		=	StaticIni.getIniObj("GuideLevel_" + nowLevel);
				nowLevel--;
				if(nowLevel<0) return;
			}
			
			++nowLevel;
			
			if(!first && levelIndex != -1)
			{
				needGuide	=	true;
				if(nowLevel>lastLevel)
				{
					setTimeout(function():void
					{
						FunctionGuideUI.getInstance().showOrHide();
					},1000);
				}
			}
			else
			{
				needGuide	=	false;
				showGuide(nowLevel);
			}
			
			first	=	false;
			lastLevel	=	nowLevel;
			NowLevel	=	nowLevel;
			isUpEffect	=	true;
		}
		
		public function showGuide(level:int):void
		{
			BottomIndexLogic.getInstance().needRefreshArrow=true;
			hideBtns();
			if(guideObj == null) return;
			
			if(specialLevelArr.indexOf(String(level)) != -1)
			{
				if(needGuide && level==7 && !first)
				{
					RightTopIndexLogic.getInstance().changeHideGlow();
				}
			}
			
			var commonBtnArr:Array		=	(guideObj["commonBtn"] as String).split(",");
			var commonBtnLen:int	=	commonBtnArr.length;
			for(var i:int=0;i<commonBtnLen;i++)
			{
				var commonDataArr:Array		=	(guideObj[commonBtnArr[i]] as String).split(",");	
				var commonBtn:SimpleButton	=	BottomIndexUI.getInstance().bottomBg.getChildByName(commonBtnArr[i]) as SimpleButton;
				var direction:String		=	"";
				if(commonBtn.x > commonDataArr[0])
				{
					direction	=	"left";
				}
				else if(commonBtn.x < commonDataArr[0])
				{
					direction	=	"right";
				}
				commonBtn.x					=	commonDataArr[0];
				commonBtn.y					=	commonDataArr[1];
				commonBtn.visible			=	true;
				if(needGuide)
				{
					if(direction=="left")
					{
						TweenLite.from(commonBtn,3,{x:"15", ease:Elastic.easeOut, overwrite:true});
					}
					else if(direction=="right")
					{
						TweenLite.from(commonBtn,3,{x:"-15", ease:Elastic.easeOut, overwrite:true});
					}
				}
			}
			
			if(guideObj["changeBtn"])
			{
				var changeBtnArr:Array	=	(guideObj["changeBtn"] as String).split(",");
				var changeBtnLen:int	=	changeBtnArr.length;
				for(var j:int=0;j<changeBtnLen;j++)
				{
					var changeDataArr:Array		=	(guideObj[changeBtnArr[j]] as String).split(",");
					var changeBtn:SimpleButton	=	BottomIndexUI.getInstance().bottomBg.getChildByName(changeBtnArr[j]) as SimpleButton;
					changeBtn.addEventListener(MouseEvent.CLICK,stopGuide);
					changeBtn.x					=	changeDataArr[0];
					changeBtn.y					=	changeDataArr[1];
					changeBtn.visible			=	true;
					
					if(needGuide)
					{
						TweenLite.from(changeBtn,2,{alpha:0, ease:Linear.easeNone, overwrite:true});
						
						guideShow.x					=	changeDataArr[0] - 25;
						guideShow.y					=	changeDataArr[1] - 23;
						
						guideShow.scaleX	=	guideShow.scaleY	=	1;
						
						guideShow.addFrameScript(guideShow.totalFrames-1,function():void
						{
							guideShow.stop();
							guideShow.visible	=	false;
							BottomIndexLogic.getInstance().needRefreshArrow=false;
						});
						guideShow.visible		=	true;
						guideShow.play();
						
//						TweenLite.from(changeBtn,4,{y:-50, ease:Elastic.easeOut, overwrite:true});
						if(level != 12)
						{
							guideArrow.visible = true;
							guideArrow.x = changeBtn.x - guideArrow.width;
							guideArrow.y = changeBtn.y + guideArrow.height*0.5;
						}
						break;
					}
				}
			}
			
			if(guideObj["arrowSkin"] && guideObj["arrowPoint"])
			{
				var arr:Array	=	String(guideObj["arrowPoint"]).split(",");
				guideArrow.visible = true;
				guideArrow.x = Number(arr[0]) - guideArrow.width;
				guideArrow.y = Number(arr[1]) + guideArrow.height*0.5;
				setTimeout(function():void
				{
					guideArrow.visible = false
				},5000);
			}
			
			
			BottomIndexLogic.getInstance().needRefreshArrow=false;
			BottomIndexLogic.getInstance().FriendTxBtn = WhisperLogic.getInstance().isHaveMsg;
		}
		
		private function stopGuide(e:MouseEvent):void
		{
			guideArrow.visible	=	false;
			if(saveGuideArr.length != 0)
			{
				var levelObj:Object	=	null
				while(levelObj == null && saveGuideArr.length != 0)
				{
					NowLevel		=	saveGuideArr.shift();
					levelObj		=	StaticIni.getIniObj("GuideLevel_" + NowLevel);
				}
				setTimeout(function():void
				{
					isUpEffect	=	false;
					levelChanged	=	NowLevel;
				},2000);
			}
		}
		
	}
}